package com.abc.tomcat;

import com.abc.servnet.NettyResponse;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.http.*;
import io.netty.util.internal.StringUtil;

import java.nio.charset.StandardCharsets;

/**
 * tomcat 对 nettyresponse 的规范实现
 */
public class DefaultNettyResponse implements NettyResponse {

    private HttpRequest request;
    private ChannelHandlerContext context;

    public DefaultNettyResponse(HttpRequest request, ChannelHandlerContext context) {
        this.request = request;
        this.context = context;
    }


    @Override
    public void write(String content) throws Exception {
        if (StringUtil.isNullOrEmpty(content)){
            return ;
        }

        //创建响应对象
        DefaultFullHttpResponse response = new DefaultFullHttpResponse(
                HttpVersion.HTTP_1_1,
                HttpResponseStatus.OK,
                //根据相应内容为 response 分配空间
                Unpooled.wrappedBuffer(content.getBytes("UTF-8"))
        );

        //获取到响应头
        HttpHeaders headers = response.headers();
        //设置响应类容类型
        headers.set(HttpHeaderNames.CONTENT_TYPE, "text/json");

        //设置响应体长豆
        headers.set(HttpHeaderNames.CONTENT_LENGTH, response.content().readableBytes());
        //设置缓存过期时长
        headers.set(HttpHeaderNames.EXPIRES, 0);
        //若http 请求连接是长连接 则设置响应连接也是长连接
        if(HttpUtil.isKeepAlive(request)){
            headers.set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE);
        }

        //将相应实例写入到Channel
        context.writeAndFlush(response);
    }
}
